local VERSION = "49";
local nkLibQuests = LibStub:NewLibrary("nkLibQuests", VERSION);

if not nkLibQuests then
    return	-- already loaded and no upgrade necessary
end

if ( GetLanguage ("player")=="DE") then
	nkLibQuests.LangText = { "%(Fertig%)", "%(Dämon%)" };
elseif ( GetLanguage("player")=="ENEU" or GetLanguage("player")=="ENUS") then
	nkLibQuests.LangText = { "%(Complete%)", "%(Demon%)" };
end;

-- Builds a complete list of all quests the player has
-- Input	None
-- Output	Table of Format:	[questID] = {index, catalogid, name, track, level, daily, exp, tp, money, detail, QuestNPCInfo {}, RewardNPCInfo {} }
--								nil if player has no quests
nkLibQuests.GetQuestList = function ()

	local NoOfQuests = GetNumQuestBookButton_QuestBook();
	
	if (NoOfQuests == 0) then
		return nil;
	 end;
	
	local QuestList = {};
	
	for idx = 1, NoOfQuests do
		local tmpQuestDetails = nkLibQuests.GetQuestDetails (idx); 
		local questID = nkLibQuests.BuildQuestIdent(tmpQuestDetails.name);		
		QuestList[questID] = tmpQuestDetails; 
	end;	
	
	return QuestList; 
	
end;

-- Compiles the detail information about a quest
-- Input:	index	- index in quest list
-- Output:	Table of format: {index, catalogid, name, track, level, daily, exp, tp, money, detail, QuestNPCInfo {}, RewardNPCInfo {} }
nkLibQuests.GetQuestDetails = function (index)
	local index, catalogID, name, track, level, daily = GetQuestInfo(index);
	
	local complete = Sol.data.quests.IsComplete(index);					
	local cleanedname = nkLibQuests.CleanQuestName(name);
		
	ViewQuest_QuestBook(index);
	local experience, talentpoints, money = GetQuestExp_QuestDetail(), GetQuestTP_QuestDetail(), GetQuestMoney_QuestDetail();
		
	local QuestNPCID = QuestDetail_GetQuestNPC();
	local RewardNPCID= QuestDetail_GetRequestQuestNPC();
		
	local infotext = GetQuestDesc_QuestDetail(1);
		
	if (infotext) then -- Thanks to pbinfo for the conversion
		infotext = string.gsub(infotext, "|Hnpc:%d+|h(.-)|h", "%1");
		infotext = string.gsub(infotext, "|c%w%w%w%w%w%w%w%w(.-)|r", "%1");
		infotext = string.gsub(infotext, "%[(.-)%]", "%1");
	end;		

	local QuestDetails = {	romID = nil;
							status = nil;
							index=index;
							catalogID=catalogID;
							name=cleanedname;
							track=track;
							level=level;
							daily=daily;
							exp=experience;
							tp=talentpoints;
							money=money;
							detail=infotext;							
						};

	local QuestNPCInfo = nil;
	
	if QuestNPCID ~= nil then
		local QuestNPC = NpcTrack_SearchNpcByDBID(QuestNPCID);
		if QuestNPC ~= nil then
			QuestNPCInfo = NpcTrack_GetNpc(QuestNPC); 
		end;
	end;
	
	local RewardNPCID = nil;
	
	if RewardNPCID ~= nil then
		local RewardNPC = NpcTrack_SearchNpcByDBID(RewardNPCID);
		if RewardNPC ~= nil then
			 RewardNPCInfo = NpcTrack_GetNpc(RewardNPC);
		end;		
	end;
	
	QuestDetails["QuestNPCInfo"] = QuestNPCInfo;
	QuestDetails["RewardNPCInfo"] = RewardNPCInfo; 
	
	if complete == true then
		QuestDetails.status = 9;
	else
		QuestDetails.status = 1;
	end;
						
	return QuestDetails;
end;

-- Builds a unique quest identification
-- Input:	questname	- the name of the quest
-- Output:	Unique quest identification
nkLibQuests.BuildQuestIdent = function (questname)
	
	local QuestIndex = Sol.data.quests.GetQuestIndex (questname);	
	local index, catalogID, name, track, level, daily = GetQuestInfo(QuestIndex);
	ViewQuest_QuestBook(QuestIndex);
	local experience, talentpoints, money = GetQuestExp_QuestDetail(), GetQuestTP_QuestDetail(), GetQuestMoney_QuestDetail();
	
	local cleanedname = nkLibQuests.CleanQuestName(name);
	
	local shortname = "";
	
	if string.find (cleanedname, " ") == nil then
		shortname = string.sub (cleanedname, 1, 2);
	else
		local tmpName = Sol.string.Split(cleanedname, " ");
		for idx = 1, #tmpName do
			shortname = shortname .. string.sub(tmpName[idx], 1, 2);
		end; 
	end;
	
	return catalogID .. ":" .. shortname .. ":" .. level .. ":" .. experience; 	
end;

-- Removes unwanted parts from quest names
-- Input:	questname	- the name of the quest
-- Output:	'cleaned' questname
nkLibQuests.CleanQuestName = function (questname)

	local newname = questname;

	-- Check if string (Complete) is part of questname
	
	for idx = 1, #nkLibQuests.LangText do
		local StrStart, StrEnd = string.find(questname, nkLibQuests.LangText[idx]);
		if (StrStart ~= nil) then
			newname = string.sub(questname, 1, StrStart-2);
		end;
	end;
	
	return newname;
end;

nkLibQuests.AddQuestToBase = function(questID, questname)

	local QuestIndex = Sol.data.quests.GetQuestIndex (questname);	
	local tmpQuestInfo = nkLibQuests.GetQuestDetails(QuestIndex);

	
	nkLibQuests_QBase[questID] = {
		name = tmpQuestInfo.name;
		catalogID = tmpQuestInfo.catalogID;
		level = tmpQuestInfo.level;
		daily = tmpQuestInfo.daily;
		experience = tmpQuestInfo.experience;
		talenpoints = tmpQuestInfo.talenpoints;
		money = tmpQuestInfo.money;
		QuestNPCInfo = tmpQuestInfo.QuestNPCInfo;
		RewardNPCInfo = tmpQuestInfo.RewardNPCInfo;
	}
	
	SaveVariables ("nkLibQuests_QBase");
	
end;

nkLibQuests.GetQuestIndexByName = function (questname)

	local NoOfQuests = GetNumQuestBookButton_QuestBook();
	
	if (NoOfQuests == 0) then
		return nil;
	 end;
	
	for idx = 1, NoOfQuests do
		local index, catalogID, name, track, level, daily = GetQuestInfo(idx);		
		if name == questname then
			return index;
		elseif nkLibQuests.CleanQuestName(name) == questname then
			return index;
		end;		 
	end;	
	
	return nil; 

end;